<template>
  <ul id="menu" v-if="isShowMenu" ref="menu" :style="{ left: x, top: y }">
    <li @click="routerPush(e.path)" v-for="e in routes" :key="e">
      {{ e.name }}
      <CheckOutlined v-show="e.path == path" />
    </li>
    <li v-if="!isFullScreen" @click="inFullscreen">进入全屏</li>
    <li v-else @click="exitFullscreen">退出全屏</li>
  </ul>
</template>

<script setup>
import { CaretRightFilled, CheckOutlined } from "@ant-design/icons-vue";
import { useRouter, useRoute } from "vue-router";
import { ref, onMounted } from "vue";
const router = useRouter();
const route = useRoute();

const routes = [
  {
    name: "综合监控",
    path: "/realTimeMonitoring/monitorall",
  },
  {
    name: "老化产线监控",
    path: "/realTimeMonitoring/monitorage",
  },
  {
    name: "老化室监控",
    path: "/realTimeMonitoring/monitorageroom",
  },
];
const showMenu = () => {
  isShowMenu.value = true;
};
const hiddenMenu = (e) => {
  isShowMenu.value = false;
};
// 存储菜单展示时的x和y位置
let x = ref();
let y = ref();
const contextmenu = (e) => {
  path.value = route.path;
  x.value = e.pageX + "px";
  y.value = e.pageY + "px";
  showMenu();
};

const routerPush = (p) => {
  path.value = p;
  router.push(p);
  hiddenMenu();
  if (p === "/") {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }
  } else {
    fullscreen();
  }
};
//控制右击菜单是否显示对号
const path = ref();
let isShowMenu = ref(false);
const menu = ref();
onMounted(() => {
  document.addEventListener("contextmenu", (e) => {
    e.preventDefault();
    path.value = route.path;
    showMenu();
  });
  document.addEventListener("keydown", (e) => {
    if (e.code === "F11") {
      e.preventDefault();
      fullscreen();
    }
  });
});
let isFullScreen = ref(false);
// 全屏
const fullscreen = () => {
  if (!document.fullscreenElement) {
    inFullscreen();
  } else {
    exitFullscreen();
  }
  console.log(isFullScreen.value);
};
const inFullscreen = () => {
  try {
    if (!document.fullscreenElement) {
      var el = document.documentElement;
      el.requestFullscreen ||
      el.mozRequestFullScreen ||
      el.webkitRequestFullscreen ||
      el.msRequestFullScreen
        ? el.requestFullscreen() ||
          el.mozRequestFullScreen() ||
          el.webkitRequestFullscreen() ||
          el.msRequestFullscreen()
        : null;
      isFullScreen.value = true;
    }
  } catch (err) {
    console.log(err);
  }
};
const exitFullscreen = () => {
  if (document.exitFullscreen) {
    document.exitFullscreen();
    isFullScreen.value = false;
  }
};

function isPageFullscreen() {
  return !!(
    document.fullscreenElement ||
    document.mozFullScreenElement ||
    document.webkitFullscreenElement ||
    document.msFullscreenElement
  );
}

// 监听全屏状态变化
document.addEventListener("fullscreenchange", function () {
  if (isPageFullscreen()) {
    console.log("页面已进入全屏模式");
    isFullScreen.value = true;
  } else {
    console.log("页面已退出全屏模式");
    isFullScreen.value = false;
  }
});
defineExpose({
  showMenu,
  hiddenMenu,
  contextmenu,
});
</script>

<style lang="scss" scoped>
#menu {
  list-style: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  width: 150px;
  position: fixed;
  z-index: 9999;
  left: 0;
  top: 0;
  box-shadow: 0 0 10px #2b5cc0;
  background-color: #205489;
  padding: 0 6px;
  border-radius: 5px;
  box-sizing: border-box;

  li {
    box-sizing: border-box;

    width: 100%;
    height: 30px;
    line-height: 30px;
    color: #fff;
    font-size: 16px;
    background-color: #205489;
    cursor: pointer;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;

    &:hover {
      background-color: #35608b;
    }

    &:not(:last-child) {
      border-bottom: 1px solid #ccc;
    }

    .second_menu {
      position: absolute;
      left: 135px;
      top: 0px;
      width: 120px;
      background-color: #205489;
      overflow: hidden;
      display: none;
      z-index: 10000;
      flex-direction: column;
      justify-content: flex-start;
      align-items: flex-start;
      // box-shadow: 0 0 10px #edeef0;

      & > div {
        box-sizing: border-box;
        padding: 0 15px;
        width: 100%;
        height: 30px;
        line-height: 30px;
        color: #fff;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        &:not(:last-child) {
          border-bottom: 1px solid #ccc;
        }
      }
    }
  }

  .show_second_menu:hover .second_menu {
    display: flex;
  }

  .show_second_menu {
    & > #setInterval {
      width: 100%;
    }
  }
}
</style>